/**
 * Created by IntelliJ IDEA
 * author: 张道阳
 * date: 2019/3/21 14:19
 * description:管理员登录
 */
package cn.mengzhiyi.guimei.dao.impl;

import cn.mengzhiyi.guimei.dao.BaseDao;
import cn.mengzhiyi.guimei.dao.LoginDao;
import cn.mengzhiyi.guimei.pojo.SuperUser;

public class LoginDaoImpl extends BaseDao implements LoginDao {

    /**
     * 管理员登录
     * @param superUser
     * @return
     */
    @Override
    public SuperUser login(SuperUser superUser) {
        SuperUser superUser1=null;
        try {
            String sql = "select * from superuser where userLoginName=?";
            Object[] params = {superUser.getUserLoginName()};
            rs = this.executeSelect(sql,params);
            //取数据
            while (rs.next()) {
                superUser1=new SuperUser();
                int id = rs.getInt("id");
                String userName = rs.getString("userName");
                String userPassword = rs.getString("userPassword");
                String userImage = rs.getString("userImage");
                int userStatus = rs.getInt("userStatus");
                String userID = rs.getString("userID");
                String userLoginName = rs.getString("userLoginName");

                superUser1.setId(id);
                superUser1.setUserName(userName);
                superUser1.setUserPassword(userPassword);
                superUser1.setUserImage(userImage);
                superUser1.setUserStatus(userStatus);
                superUser1.setUserID(userID);
                superUser1.setUserLoginName(userLoginName);

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeConnection();
        }
        return superUser1;
    }

    /**
     * 根据ID查询管理员信息
     * @param userid
     * @return
     */
    @Override
    public SuperUser getSuperUserByID(int userid) {
        SuperUser superUser1=null;
        try {
            String sql = "select * from superuser where id=?";
            Object[] params = {userid};
            rs = this.executeSelect(sql,params);
            //取数据
            while (rs.next()) {
                superUser1=new SuperUser();
                int id = rs.getInt("id");
                String userName = rs.getString("userName");
                String userPassword = rs.getString("userPassword");
                String userImage = rs.getString("userImage");
                int userStatus = rs.getInt("userStatus");
                String userID = rs.getString("userID");
                String userLoginName = rs.getString("userLoginName");
                superUser1.setId(id);
                superUser1.setUserName(userName);
                superUser1.setUserPassword(userPassword);
                superUser1.setUserImage(userImage);
                superUser1.setUserStatus(userStatus);
                superUser1.setUserID(userID);
                superUser1.setUserLoginName(userLoginName);

            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeConnection();
        }
        return superUser1;
    }

    /**
     * 更新管理员信息
     * @param superUser
     * @return
     */
    @Override
    public int updateOther(SuperUser superUser) {
        int ir=0;
        String sql="update superuser set userName=?,userID=?,userLoginName=? where id=?";
        Object[] params = {superUser.getUserName(),superUser.getUserID(),superUser.getUserLoginName(),superUser.getId()};
        ir = this.executeUpdateRows(sql,params);
        return ir;
    }

    /**
     * 更新管理员密码
     * @param superUser
     * @return
     */
    @Override
    public int updatePwd(SuperUser superUser) {
        return 0;
    }
}
